package IBBB.view;

import IBBB.model.UserMessage;
import javafx.beans.binding.NumberBinding;
import javafx.beans.property.*;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Side;
import javafx.scene.Parent;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;

import java.io.IOException;

public class ScheduleBox {
    @FXML
    private Text wordStockName;
    @FXML
    private Text scheduleText;
    @FXML
    private Text totalWordsNum;
    @FXML
    private Text planText;
    @FXML
    private ImageView wordStockImage;
    @FXML
    private PieChart scheduleChart;
    @FXML
    private Button startLearnButton;
    @FXML
    private Button startReviewButton;
    private NumberBinding part;
    private ObservableList<PieChart.Data> data;
    private StackPane container;
    private UserMessage userMessage;


    public void setScheMessage(StringProperty name, IntegerProperty sche, Integer totalNum, IntegerProperty plan) {
        wordStockName.setText(name.getValue());
        scheduleText.setText(String.valueOf(sche.get()));
        totalWordsNum.setText(String.valueOf(totalNum));
        planText.setText(String.valueOf(plan.get()));
        wordStockName.textProperty().bind(name);
        sche.addListener((observableValue, number, t1) -> scheduleText.setText(String.valueOf(t1)));
        //totalNum.addListener((observableValue, number, t1) -> totalWordsNum.setText(String.valueOf(t1)));
        plan.addListener((observableValue, number, t1) -> planText.setText(String.valueOf(t1)));
        part = sche.divide((double)totalNum);
        data = FXCollections.observableArrayList(
                new PieChart.Data("已学习", (double)part.getValue()),
                new PieChart.Data("未学习", 1 - (double)part.getValue())
        );
        data.get(0).pieValueProperty().bind(part);
        data.get(1).pieValueProperty().bind(part.multiply(-1).add(1));
        scheduleChart.setData(data);
        scheduleChart.setLegendSide(Side.LEFT);
        scheduleChart.setTitle("学习进度");
        sche.addListener((observableValue, number, t1) -> {
            if ((t1).equals(totalNum))
                startLearnButton.setDisable(true);
        });
        wordStockImage.setImage(new Image(wordStockName.getText() + ".png"));
    }

    public void setCaller(StackPane caller, UserMessage userMessage) {
        this.userMessage = userMessage;
        container = caller;
        if (userMessage.getCurrentSchedule(wordStockName.getText()) == 0) {
            startReviewButton.setDisable(true);
        }
    }

    @FXML
    private void setStartLearnButtonAction() {
        //System.out.println("start");
        Parent node = null;
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("LearnPane.fxml"));
        try {
            node = loader.load();
            LearnPane controller = loader.getController();
            controller.init(container, userMessage, wordStockName.getText(),false);
        } catch (IOException e) {
            //TODO: handle exception
            e.printStackTrace();
        }
        container.getChildren().get(container.getChildren().size() - 1).setVisible(false);
        container.getChildren().add(node);
    }

    @FXML
    private void  setStartReviewButtonAction() {
        Parent node = null;
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("LearnPane.fxml"));
        try {
            node = loader.load();
            LearnPane controller = loader.getController();
            controller.init(container, userMessage, wordStockName.getText(), true);
        } catch (IOException e) {
            //TODO: handle exception
            e.printStackTrace();
        }
        container.getChildren().get(container.getChildren().size() - 1).setVisible(false);
        container.getChildren().add(node);
    }
}
